package com.aios.dao;

import com.aios.entity.SystemUser;
import com.aios.entity.SystemUserExample;

import java.util.List;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

public interface SystemUserMapper {
    
    int countByExample(SystemUserExample example);
    int deleteByExample(SystemUserExample example);
    int deleteByPrimaryKey(Integer id);
    int insert(SystemUser record);
    int insertWithoutID(SystemUser record);
    int insertSelective(SystemUser record);
    List<SystemUser> selectByExample(SystemUserExample example);
    SystemUser selectByPrimaryKey(Integer id);
    int updateByExampleSelective(@Param("record") SystemUser record, @Param("example") SystemUserExample example);
    int updateByExample(@Param("record") SystemUser record, @Param("example") SystemUserExample example);
    int updateByPrimaryKeySelective(SystemUser record);
    int updateByPrimaryKey(SystemUser record);
        
    @Select("SELECT * FROM system_user WHERE EMAIL = #{username} AND USER_PASSWORD=#{password} AND STATUS=#{status}")
   	@ResultMap(value="MiniResultMap")
   	List<SystemUser> findByEmailAndPassword(
   			@Param("username") String username,
   			@Param("password") String password,
   			
   			@Param("status") Integer status);
    
    @Select("SELECT * FROM system_user WHERE STATUS = #{status} ORDER BY SORT_ORDER")
   	@ResultMap(value="MiniResultMap")
   	List<SystemUser> findAllByStatus(@Param("status") Integer status);
    
    @Select("SELECT * FROM system_user WHERE EMAIL = #{email}")
   	@ResultMap(value="MiniResultWithPasswordMap")
   	List<SystemUser> findByEmailAddress(@Param("email") String email);
    
    @Select("SELECT COUNT(*) FROM system_user WHERE STATUS = #{status}")
	Integer countAllByStatus(@Param("status") Integer status);
    
    @Select("SELECT * FROM system_user WHERE STATUS = #{status} ORDER BY SORT_ORDER LIMIT #{start}, #{size}")
   	@ResultMap(value="MiniResultMap")
   	List<SystemUser> findListPaginated(@Param("status") Integer status, 
			@Param("start") Integer start, @Param("size") Integer size);
    
    //@Select("SELECT ID,NAME,LAST_LOGIN FROM system_user WHERE LAST_LOGIN = (SELECT max(LAST_LOGIN) FROM system_user)")
    @Select("SELECT ID,NAME,LAST_LOGIN FROM system_user order by LAST_LOGIN DESC limit 1")
    @ResultMap(value="LastLoginResultMap")
	SystemUser findLastLogin();
    
    @Select("SELECT * FROM system_user WHERE EMAIL=#{email} AND ID != #{id}")
    @ResultMap(value ="MiniResultMap")
    List<SystemUser> findEmailNotWithId(@Param("email") String email, @Param("id") Integer id);
  
    @Select("SELECT * FROM system_user WHERE EMAIL = #{username} AND USER_PASSWORD=#{password} ")
   	@ResultMap(value="MiniResultMap")
    List<SystemUser> findByEmailAndPasswordWithoutStatus(
    		@Param("username") String email,
    		@Param("password") String password);
    
	@Update("UPDATE system_user SET STATUS=#{alertStatus} WHERE ID=#{systemUserId} ")
	void updateStatusByPrimaryKey(@Param("systemUserId") Integer systemUserId,@Param("alertStatus") Byte alertStatus);
    
	
    
	
 

    
    
    
}